package com.yzc.controller;

import com.yzc.model.query.system.UserInfoReq;
import com.yzc.model.vo.system.SysUserVo;
import com.yzc.model.vo.system.UserInfoVo;
import com.yzc.result.Result;
import com.yzc.result.ResultCodeEnum;
import com.yzc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/userapi")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 获取用户数据
     */
    @PostMapping("/loadAll")
    public Result<List<UserInfoVo>> loadAll() {
        log.info( "加载用户数据");
        return Result.success(userService.loadaAll());
    }


    /**
     * 新增用户数据
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PutMapping("/saveUser")
    public Result<ResultCodeEnum> saveUser(@RequestBody UserInfoReq userInfoReq) {
        return Result.success(userService.saveUser(userInfoReq));
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PutMapping("/updateUser")
    public Result<ResultCodeEnum> updateUser(@RequestBody UserInfoReq userInfoReq) {
        return Result.success(userService.updateUser(userInfoReq));
    }

    /**
     * 删除用户数据
     *
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/delete/{id}")
    public Result deleteInfo(@PathVariable("id") Long id) {
        log.info("删除用户数据");
        return Result.success(userService.removeById(id));
    }

    /**
     * 根据用户名查询用户数据
     *
     * @return
     */
    @GetMapping("/selectInfos")
    public Result<SysUserVo> selectInfos(@RequestParam("username") String username) {
        log.info("查询用户数据");
        return Result.success(userService.getUserVoByName(username));
    }

    /**
     * 根据id查询用户数据
     *
     * @return
     */
    @GetMapping("/getInfoById")
    public Result<UserInfoVo> getInfoById(@RequestParam("id") Long id) {
        log.info("根据id查询用户数据");
        return Result.success(userService.getInfoById(id));
    }


        /*@PutMapping("/updateXinxi")
    public Result updateXinxi(@RequestBody SysUser sysUser) {
        userService.updateXinxi(sysUser);
        log.info(LOG_MARKER,"修改用户信息");
        return Result.success() ;
    }*/

   /* @PutMapping("/updatePass")
    public Map updatePass(@RequestBody UserBean userBean) {
        log.info(LOG_MARKER,"修改用户密码");
        return userService.updatePass(userBean);
    }*/


}